package com.cool.cloud.service.impl;

import com.cool.cloud.entity.FileShare;
import com.cool.cloud.service.ShareFileService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author laijunji
 * @ClassName ShareFileServiceImpl
 * @Description (TempFile)表服务实现类
 * @date 2020-03-10 11:22:11
 * @Version 1.0
 **/
@Service("shareFileService")
public class ShareFileServiceImpl extends BaseService implements ShareFileService {

    /**
     * @param fileShare 实例对象
     * @return boolean
     * @Description 添加fileShare
     * @author laijunji
     * @date 2020-03-10 11:22:11
     */
    @Override
    public boolean insert(FileShare fileShare) {
        if (shareFileMapper.insert(fileShare) == 1) {
            return true;
        }
        return false;
    }

    /**
     * @param fileShare
     * @Description 删除fileShare
     * @author laijunji
     * @date 2020-03-10 11:22:11
     */
    @Override
    @Async
    public void deleteByCon(FileShare fileShare) throws InterruptedException {
        try {
            if (fileShare.getEndDateType() == 1) {
                TimeUnit.MINUTES.sleep(5);
            } else if (fileShare.getEndDateType() == 2) {
                TimeUnit.HOURS.sleep(1);
            } else if (fileShare.getEndDateType() == 3) {
                TimeUnit.HOURS.sleep(24);
            } else if (fileShare.getEndDateType() == 4) {
                TimeUnit.DAYS.sleep(7);
            } else if (fileShare.getEndDateType() == 5) {
                TimeUnit.DAYS.sleep(30);
            }
        } catch (InterruptedException e) {
            throw new InterruptedException(e.getMessage());
        }
        shareFileMapper.deleteByCon(fileShare);
    }

    /**
     * @param id
     * @Description 删除fileShare
     * @author laijunji
     * @date 2020-03-10 11:22:11
     */
    @Override
    public boolean deleteByFileId(Integer id) {
        if (shareFileMapper.deleteByFileId(id) == 1) {
            return true;
        }
        return false;
    }

    /**
     * @return 对象列表
     * @Description 查询全部数据
     * @author laijunji
     * @date 2020-03-10 11:22:11
     * 分页使用MyBatis的插件实现
     */
    @Override
    public List<FileShare> queryAll() {
        return shareFileMapper.queryAll();
    }

    /**
     * @param fileid 主键
     * @return FileShare
     * @Description 查询某一数据
     * @author laijunji
     * @date 2020-03-10 11:22:11
     */
    @Override
    public FileShare queryByFileId(Integer fileid) {
        return shareFileMapper.queryByFileId(fileid);
    }

    /**
     * @param userid
     * @return 对象列表
     * @Description 查询某一数据
     * @author laijunji
     * @date 2020-03-10 11:22:11
     */
    @Override
    public List<FileShare> queryByUserId(Integer userid) {
        return shareFileMapper.queryByUserId(userid);
    }

    /**
     * @param 全局UUID
     * @return FileShare
     * @Description 查询某一数据
     * @author laijunji
     * @date 2020-03-10 11:22:11
     */
    @Override
    public FileShare queryByUrl(String url) {
        return shareFileMapper.queryByUrl(url);
    }

    /**
     * @return 影响行数
     * @Description 修改fileShare
     * @author laijunji
     * @date 2020-03-10 11:22:11
     */
    @Override
    public boolean updateByFileId(FileShare fileShare) {
        if (shareFileMapper.updateByFileId(fileShare) == 1) {
            return true;
        }
        return false;
    }

    /**
     * @return 影响行数
     * @Description 批量删除
     * @author laijunji
     * @date 2020-03-10 11:22:11
     */
    @Override
    public int batchDelete(String[] ids) {
        return shareFileMapper.batchDelete(ids);
    }
}